package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Patient;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.*;
import com.jrtc.service.PatientService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 患者表(Patient)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("patient")
public class PatientController {
    /**
     * 服务对象
     */
    @Resource
    private PatientService patientService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Patient selectOne(Integer id) {
        return this.patientService.queryById(id);
    }
    
    @RequestMapping("getPatientList")
    public ResultDTO getPatientList(@RequestBody Patient patient)
    {
        IPage<Patient> ipage = patientService.queryPatientList(patient);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertPatient")
    public ResultDTO insertPatient(@RequestBody Patient patient)
    {
        Patient tmpPatient = patientService.insert(patient);
        if (tmpPatient != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getPatientById")
    public ResultDTO getPatientById(@RequestBody Map<String,Integer> map)
    {
        int patientId = map.get("patientId");
        Patient patient = patientService.queryById(patientId);
        if (patient==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(patient);
    }

    @RequestMapping("updatePatient")
    public ResultDTO updatePatient(@RequestBody Patient patient)
    {
        if (patient==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (patientService.update(patient)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deletePatient")
    public ResultDTO deletePatient(@RequestBody Map<String,Integer> map)
    {
        int patientId = map.get("patientId");
        if (patientService.deleteById(patientId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 查询患者列表
     * */
    @PostMapping("/patientList")
    public ResultDTO patientList(@RequestBody Map<String, Object> map){
        int pageNum = 1;
        int pageSize = 10;
        if (map.get("pageNum") != null && !map.get("pageNum").equals("") && map.get("pageSize") != null && !map.get("pageSize").equals("")){
            pageNum = Integer.parseInt(map.get("pageNum").toString());
            pageSize = Integer.parseInt(map.get("pageSize").toString());
        }
        String patientName = null;
        if (map.get("patientName") != null && !map.get("patientName").equals("")){
            patientName = map.get("patientName").toString();
        }
        List<PatientVo> patientVoList;
        try {
            patientVoList = patientService.patientList(patientName, pageNum, pageSize);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
        if (patientVoList != null && patientVoList.size() > 0){
            return ResultDTOBuilder.success(patientVoList);
        }
        else {
            return ResultDTOBuilder.failureMsg("未查到数据");
        }
    }

    /**
     * 创建新病人
     * */
    @PostMapping("/createNewPatient")
    public ResultDTO createNewPatient(HttpServletRequest request, @RequestBody Patient patient){
        if (patient != null){
            int newPatient;
            try {
                newPatient = patientService.createNewPatient(patient, request);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (newPatient > 0){
                return ResultDTOBuilder.success("添加成功");
            }
            else {
                return ResultDTOBuilder.failureMsg("添加失败");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }

    /**
     * 患者账号密码登录
     */
    @PostMapping("/patientLogin")
    public ResultDTO login(@RequestBody PatientLoginVo patientLoginVo) {
        Patient patient = patientService.login(patientLoginVo);
        if (patient == null) {
            return ResultDTOBuilder.failure("01000","账号或密码不正确！");
        }
        return ResultDTOBuilder.success(patient);
    }

    /**
     * 补充患者信息
     * */
    @PostMapping("/selectPatient")
    public ResultDTO selectPatient(HttpServletRequest request, @RequestBody PatientParameterVo patientParameterVo) {
        if (patientParameterVo.getHeight() != null && patientParameterVo.getHeight() != 0 &&
                patientParameterVo.getWeight() != null && patientParameterVo.getWeight() != 0 &&
                patientParameterVo.getRestingHeartRate() != null && patientParameterVo.getRestingHeartRate() != 0 &&
                patientParameterVo.getSportsTargetList() != null && patientParameterVo.getSportsTargetList().size() > 0 &&
                patientParameterVo.getStandardOfLiving() != null && !patientParameterVo.getStandardOfLiving().equals("")){

            Integer patientId;
            try {
                patientId = patientService.getLoginPatient(request).getPatientId();
            } catch (Exception e){
                return null;
            }
            patientParameterVo.setPatientId(patientId);
            int i;
            try {
                i = patientService.selectPatient(patientParameterVo);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (i > 0){
                return ResultDTOBuilder.success("添加成功");
            }
            else {
                return ResultDTOBuilder.failureMsg("信息记录失败");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }

    /**
     * 患者详细信息
     * */
    @PostMapping("/patientDetailed")
    public ResultDTO patientDetailed(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());

        PatientDetailedVo patientDetailedVo = patientService.patientDetailed(id);
        return ResultDTOBuilder.success(patientDetailedVo);
    }

    /**
     * 查询待修改的患者信息
     * */
    @PostMapping("/selPatient")
    public ResultDTO selPatient(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        Patient patient = patientService.selPatient(id);
        return ResultDTOBuilder.success(patient);
    }

    /**
     * 修改患者信息
     * */
    @PostMapping("/savePatient")
    public ResultDTO savePatient(@RequestBody Patient patient){
        int i = patientService.savePatient(patient);
        if (i > 0){
            return ResultDTOBuilder.success("修改成功");
        }
        else {
            return ResultDTOBuilder.failure("修改失败");
        }
    }

    /**
     * 患者抓小球需完成数量
     * */
    @PostMapping("/needBallCount")
    public ResultDTO needBallCount(@RequestBody Map<String, Object> map){
        String idCard = map.get("idCard").toString();
        Integer count = patientService.needBallCount(idCard);
        return ResultDTOBuilder.success(count);
    }

    /**
     * 患者管理
     * */
    @PostMapping("/management")
    public ResultDTO management(HttpServletRequest request, @RequestBody ManagementVo managementVo){
        IPage<Patient> patient;
        try {
            patient = patientService.management(request, managementVo);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(patient);
    }

    /**
     * 查询待修改的患者信息
     * */
    @PostMapping("/patientDetails")
    public ResultDTO patientDetails(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        Patient patient = patientService.patientDetails(id);
        return ResultDTOBuilder.success(patient);
    }

    /**
     * 修改患者信息
     * */
    @PostMapping("/editPatient")
    public ResultDTO editPatient(@RequestBody Patient patient){
        int i = patientService.editPatient(patient);
        if (i > 0){
            return ResultDTOBuilder.success("修改成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("修改失败");
        }
    }

    /**
     * 患者上传头像
     * */
    @PostMapping("/patientAvatar")
    public ResultDTO patientAvatar(MultipartFile file){
        String string;
        try {
            string = patientService.puploadFile(file);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(string);
    }
}